import time, datetime
#datetime.datetime 转换为 str
t = datetime. datetime. now(tz = None)
print(t.strftime("%Y-%m-%d %H:%M:%S"))

# str 转换为 datetime. datetime
t = datetime. datetime. now(tz = None)
a = t. strftime("%Y-%m-%d %H:%M:%S")
b = datetime. datetime. strptime(a, "%Y-%m-%d %H:%M:%S")
print(b)

#datetime.datetime 转换为 timestamp
t = datetime. datetime. now(tz = None)
c = t.timestamp()
print(c)

#timestamp 转换为 datetime. datetime
d = datetime. datetime. fromtimestamp(c, tz = None)
print(d)

#timestamp 转换为 str
e = datetime. datetime. fromtimestamp(c, tz = None)
f = e. strftime("%Y-%m-%d %H:%M:%S")
print(f)

#  str 转换为  timestamp
g = datetime. datetime. strptime(f, "%Y-%m-%d %H:%M:%S")
h = g. timestamp()
print(h)

#封装函数
import datetime
def get_date():
    n = input("若想知道当前日期，请输入-1：")
    if n == "-1":
        t = datetime.datetime.now(tz=None)
        print(t.strftime("%Y-%m-%d"))
    else:
        print("您输入的不是-1，请重新输入！")
        get_date()
    return

get_date()

